package com.qiudaozhang.jblog.backend.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qiudaozhang.jblog.model.User;
import com.qiudaozhang.jblog.user.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 邱道长
 * 2018/12/13 11:01
 */
@Controller
public class LoginController {


    @Reference(url = "${user.service.provider.url}", version = "${service.version}", group = "${dubbo.group}")
    private UserService userService;

    @RequestMapping("to/login")
    public String toLogin(){
        return "admin/login";
    }

    @RequestMapping("login")
    public String login(User user, HttpServletRequest request){
        User u = userService.queryByUsername(user);
        if(u != null){
            if(u.getPassword().equals(user.getPassword())){
                // 登录成功
                u.setPassword(null);
                request.getSession().setAttribute("user",u);
                return "admin/main";
            } else {
                // 密码错误
                return "admin/login";
            }
        } else {
            // 用户名不存在
            return "admin/login";
        }
    }

}
